package com.example.jiwhsgs;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class XSactivity extends Activity {

    private SQLiteDatabase myDatabase;
    private RadioGroup radioGroup;
    private List<XSData> currentList; // 存储当前的 List<XSData>
    private Button confirmButton;

    private RadioButton radioButton1;
    private TextView radioText1;
    private RadioButton radioButton2;
    private TextView radioText2;
    private RadioButton radioButton3;
    private TextView radioText3;
    private String selectedChoiceIndex = "null"; // 用于存储选中的name

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xs);

        myDatabase = openOrCreateDatabase("sgsdb.db", MODE_PRIVATE, null);
        List<XSData> startSkills = getData("start_xs");
        List<XSData> endSkills = getData("end_xs");
        List<XSData> injuredSkills = getData("injured_xs");

        confirmButton = findViewById(R.id.xs_confirm);
        radioButton1 = findViewById(R.id.radioButton1);
        radioText1 = findViewById(R.id.radioText1);
        radioButton2 = findViewById(R.id.radioButton2);
        radioText2 = findViewById(R.id.radioText2);
        radioButton3 = findViewById(R.id.radioButton3);
        radioText3 = findViewById(R.id.radioText3);

        Button startButton = findViewById(R.id.start_xs);
        Button endButton = findViewById(R.id.end_xs);
        Button injuredButton = findViewById(R.id.injured_xs);
        radioGroup = findViewById(R.id.radioGroup);

        // 设置 RadioGroup 的监听器
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId != -1) {
                selectedChoiceIndex = getSelectedIndex(checkedId);
            }
        });

        confirmButton.setOnClickListener(v -> {
            if (!Objects.equals(selectedChoiceIndex, "null")) {
                handleSelectedSkill(currentList);
                startButton.setEnabled(true);
                endButton.setEnabled(true);
                injuredButton.setEnabled(true);
            } else {
                Log.d("XSactivity", "No skill selected or currentChoices is null");
            }
        });

        startButton.setOnClickListener(v -> {
            randomSkill(startSkills);
            startButton.setEnabled(false);
            endButton.setEnabled(true);
            injuredButton.setEnabled(true);
        });
        endButton.setOnClickListener(v -> {
            randomSkill(endSkills);
            endButton.setEnabled(false);
            injuredButton.setEnabled(true);
            startButton.setEnabled(true);
        });
        injuredButton.setOnClickListener(v -> {
            randomSkill(injuredSkills);
            injuredButton.setEnabled(false);
            startButton.setEnabled(true);
            endButton.setEnabled(true);
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myDatabase != null && myDatabase.isOpen()) {
            myDatabase.close();
        }
    }

    private String getSelectedIndex(int checkedId) {
        for (int i = 0; i < radioGroup.getChildCount(); i += 2) {
            RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
            if (radioButton.getId() == checkedId) {
                return radioButton.getText().toString();
            }
        }
        return "null";
    }

    private void randomSkill(List<XSData> skillList) {
        List<Integer> selectedIndices = new ArrayList<>();
        int randomIndex;
        int count = skillList.size();

        for (int i = 0; i < 3; i++) {
            randomIndex = (int) (Math.random() * skillList.size());
            while (skillList.get(randomIndex).is_selected && count > 0) {
                randomIndex = (randomIndex + 1) % skillList.size();
                count--;
            }
            if (count != 0) {
                selectedIndices.add(randomIndex);
                skillList.get(randomIndex).is_selected = true;
                count = skillList.size();
            } else {
                break;
            }
        }

        for (int index : selectedIndices) {
            skillList.get(index).is_selected = false;
        }

        setThreeChoices(skillList, selectedIndices);
    }

    private void setThreeChoices(List<XSData> skillList, List<Integer> indices) {
        this.currentList = skillList;
        TextView selectedSkillTextView = findViewById(R.id.xs_selectedSkill);

        if (!indices.isEmpty()) {
            selectedSkillTextView.setText("请选择技能");
            setChoice(radioButton1, radioText1, skillList, indices.get(0));
            if (indices.size() >= 2) {
                setChoice(radioButton2, radioText2, skillList, indices.get(1));
                if (indices.size() >= 3) {
                    setChoice(radioButton3, radioText3, skillList, indices.get(2));
                }
            }
        } else {
            selectedSkillTextView.setText("技能池已空！！");
        }

        confirmButton.setVisibility(View.VISIBLE);
    }

    private void setChoice(RadioButton radioButton, TextView textView, List<XSData> skillList, int index) {
        radioButton.setText(skillList.get(index).name);
        textView.setText(skillList.get(index).detail);
        radioButton.setVisibility(View.VISIBLE);
        textView.setVisibility(View.VISIBLE);
    }

    private void handleSelectedSkill(List<XSData> skillList) {
        TextView selectedSkillTextView = findViewById(R.id.xs_selectedSkill);
        for (XSData skill : skillList) {
            if (Objects.equals(skill.name, selectedChoiceIndex)) {
                skill.is_selected = true;
                String selectedSkillText = getString(R.string.selected_skill, skill.name, skill.detail);
                selectedSkillTextView.setText(selectedSkillText);
                hideChoices();
                radioGroup.clearCheck();
                return;
            }
        }
        selectedChoiceIndex = "null";
    }


    private void hideChoices() {
        radioButton1.setVisibility(View.GONE);
        radioText1.setVisibility(View.GONE);
        radioButton2.setVisibility(View.GONE);
        radioText2.setVisibility(View.GONE);
        radioButton3.setVisibility(View.GONE);
        radioText3.setVisibility(View.GONE);
        confirmButton.setVisibility(View.GONE);
    }

    private List<XSData> getData(String tableName) {
        List<XSData> skillList = new ArrayList<>();
        String query = "SELECT * FROM " + tableName;
        try (Cursor cursor = myDatabase.rawQuery(query, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                int nameIndex = cursor.getColumnIndex("skill");
                int detailIndex = cursor.getColumnIndex("detail");

                do {
                    XSData skillData = new XSData();
                    skillData.name = cursor.getString(nameIndex);
                    skillData.detail = cursor.getString(detailIndex);
                    skillData.is_selected = false;
                    skillList.add(skillData);
                } while (cursor.moveToNext());
            }
        }
        return skillList;
    }

    public static class XSData {
        public String name;
        public String detail;
        public boolean is_selected = false;
    }
}
